package com.ives.launcher.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;

import com.ives.launcher.R;
import com.ives.launcher.model.ItemInfo;
import com.ives.launcher.model.ItemInfo_App;
import com.ives.launcher.model.ItemInfo_Floder;
/**
 * 图片缩放工具类
 * @author Robert
 *
 */
public class ImageTool {
	/**
	 * 根据给定给定宽度和高度生成图片
	 * @param source
	 * @param width
	 * @param height
	 * @return
	 */
	public static Bitmap getScaleBitmap(Bitmap source,float width,float height){
		 //计算缩放率，新尺寸除原始尺寸   
        float scaleWidth = width/  source.getWidth();   
        float scaleHeight =  height/ source.getHeight();    
        // 创建操作图片用的matrix对象   
        Matrix matrix = new Matrix();   
        // 缩放图片动作   
        matrix.postScale(scaleWidth, scaleHeight);   
        // 创建新的图片   
        Bitmap resizedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);   
		return resizedBitmap;
	}
	/**
	 * 根据给定给定宽度按比例生成图片
	 * @param source
	 * @param width
	 * @return
	 */
	public static Bitmap getScaleBitmapToWidth(Bitmap source,float width){
		float height = width/source.getWidth()*source.getHeight();
        //计算缩放率，新尺寸除原始尺寸   
        float scaleWidth = width /  source.getWidth();   
        float scaleHeight =  height / source.getHeight();   
        // 创建操作图片用的matrix对象   
        Matrix matrix = new Matrix();   
        // 缩放图片动作   
        matrix.postScale(scaleWidth, scaleHeight);   
        // 创建新的图片   
        Bitmap resizedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);   
		return resizedBitmap;
	}
	/**
	 * 根据给定给定高度按比例生成图片
	 * @param source
	 * @param height
	 * @return
	 */
	public static Bitmap getScaleBitmapToHeight(Bitmap source,float height){
		float width = height/source.getHeight()*source.getWidth();
		 //计算缩放率，新尺寸除原始尺寸   
        float scaleWidth = width /  source.getWidth();   
        float scaleHeight = height / source.getHeight();   
        // 创建操作图片用的matrix对象   
        Matrix matrix = new Matrix();   
        // 缩放图片动作   
        matrix.postScale(scaleWidth, scaleHeight);   
        // 创建新的图片   
        Bitmap resizedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);   
		return resizedBitmap;
	}
	/**
	 * 根据比例求出新图片
	 * @param source
	 * @param ratio
	 * @return
	 */
	public static Bitmap getScaleBitmapToWidthRatio(Bitmap source,float ratio){
        // 创建操作图片用的matrix对象   
        Matrix matrix = new Matrix();   
        // 缩放图片动作   
        matrix.postScale(ratio, ratio);   
        // 创建新的图片   
        Bitmap resizedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);   
		return resizedBitmap;
	}
	/** 
     * Drawable 转 bitmap 
     * @param drawable 
     * @return 
     */  
    public static Bitmap drawableToBitmap(Drawable drawable){  
        if(drawable instanceof BitmapDrawable){  
            return ((BitmapDrawable)drawable).getBitmap() ;  
        }else if(drawable instanceof NinePatchDrawable){  
            Bitmap bitmap = Bitmap  
                    .createBitmap(  
                            drawable.getIntrinsicWidth(),  
                            drawable.getIntrinsicHeight(),  
                            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                                    : Bitmap.Config.RGB_565);  
            Canvas canvas = new Canvas(bitmap);  
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),  
                    drawable.getIntrinsicHeight());  
            drawable.draw(canvas);  
            return bitmap;  
        }else{  
            return null ;  
        }  
    }
    
    /**
	 * 根据不同的imteinfo类型生成不同对显示图片
	 * @param info
	 * @return
	 */
	public static Bitmap getShowBitmap(ItemInfo info,Context context){
		Bitmap icon = null;
		if(info.itemType==ItemInfo.Type_App){
			icon = ((ItemInfo_App)info).icon;
		}else if(info.itemType==ItemInfo.Type_Floder){
			//制作文件夹的显示图片
			return makeFloderIcon((ItemInfo_Floder) info,context);
		}else if(info.itemType==ItemInfo.Type_Widget){
			
		}
		return icon;
	}
	
	/**
	 * 根据文件夹信息制作文件夹显示图标
	 * @param floder
	 * @return
	 */
	public static  Bitmap makeFloderIcon(ItemInfo_Floder floder,Context context){
		//如果文件夹有自定义的icon
		if(floder.customIcon!=null)return floder.customIcon;
		Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		Paint paint = new Paint(); 
		//画出背景
		paint.setColor(Color.DKGRAY);
		RectF roundRect = new RectF(0f,0f,100f,100f);
		canvas.drawRoundRect(roundRect, 10f, 10f, paint);
		int count = 0;
		point:
		for (int i = 0; i < 2; i++) {//行
			for (int j = 0; j < 2; j++) {//列
				if(count==floder.items.size())break point;
				Bitmap b = floder.items.get(count).icon;
				Rect src = new Rect(0, 0, b.getWidth(),b.getHeight());
				Rect dst = new Rect(j*50,i*50,(j+1)*50,(i+1)*50);
				canvas.drawBitmap(b, src, dst, paint);
				count++;
			}
		}
		return bitmap;
	}
}
